﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using cn.edu.jnmc.pb;
using System.Data.OleDb;

public partial class _Default : System.Web.UI.Page 
{
    public int userCounter;
    private ArrayList articleList;
    private const int ARTICLE_NUM = 6;
    private string[] articleTitles;
    private OleDbConnection conn;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            conn = DatabaseService.DBopen();
            initList();
            bindResultData();
        }
    }

    private void bindResultData()
    {
        articleList = new ArrayList();       
        string resultSql = "select article_id, avg(student_score) as s_score, avg(teacher_score) as t_score from article_score group by article_id";
        OleDbDataReader reader = DatabaseService.excuteQuerySQLOnline(resultSql, conn);
        if (reader.HasRows)
        {
            int i = 0;
            while (reader.Read())
            {
                Article article = new Article();
                article.Name = articleTitles[i];
                article.Id = int.Parse(reader["article_id"].ToString());
                article.TeacherScore = double.Parse(reader["t_score"].ToString());
                article.StudentScore = double.Parse(reader["s_score"].ToString());

                i++;
                articleList.Add(article);
            }
        }
        else
        {
            for (int i = 0; i < ARTICLE_NUM; i++)
            {
                Article article = new Article();
                article.Name = articleTitles[i];
                article.Id = i+1;
                article.TeacherScore =0;
                article.StudentScore = 0;

                articleList.Add(article);
            }
        }
        reader.Close();        

        ResultRepeater.DataSource = articleList;
        ResultRepeater.DataBind();
    }
    
    private void initList()
    {
        string countSql = "select count(*) as count_num from article_score where article_id=1";

        articleTitles = new string[ARTICLE_NUM]
        {
            "GSZ 英语翻译竞赛参评译文",
            "GZG 英语翻译竞赛参评译文",
            "LLM 英语翻译竞赛参评译文",
            "LXM 英语翻译竞赛参评译文",
            "LYF 英语翻译竞赛参评译文",
            "XS 英语翻译竞赛参评译文"
        };

        OleDbDataReader reader = DatabaseService.excuteQuerySQLOnline(countSql, conn);
        if (reader.HasRows)
        {
            while (reader.Read())
            {
                userCounter = int.Parse(reader["count_num"].ToString());
            }
        }
        reader.Close();
    }
}